<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<div class="container">
  <d-breadcrumb>
    <d-breadcrumb-item>
      <a routerLink="/kie">{{ "kie.create.title" | translate }}</a>
    </d-breadcrumb-item>
    <d-breadcrumb-item>
      <span>
        {{
          (type === "eidt"
            ? "kie.create.eidteConfig"
            : "kie.create.createConfig"
          ) | translate
        }}
      </span>
    </d-breadcrumb-item>
  </d-breadcrumb>

  <form
    class="cse-mg-top-md"
    dForm
    ngForm
    [formGroup]="formGroup"
    #form="dValidateRules"
    [dValidateRules]="formRules.rule"
    [layout]="FormLayout.Horizontal"
    [labelSize]="'sm'"
  >
    <d-form-item *ngIf="configType">
      <d-form-label [required]="false">
        {{ "kie.create.type" | translate }}
      </d-form-label>
      <d-form-control>{{ configType }}</d-form-control>
    </d-form-item>
    <d-form-item *ngIf="configType === 'app' && type === 'create'">
      <d-form-label [required]="true">
        {{ "kie.create.app" | translate }}
      </d-form-label>
      <d-form-control>
        <span *ngIf="appId">{{ appId }}</span>
        <i
          *ngIf="appId"
          class="icon icon-delete app-delete"
          (click)="onDeleteApp()"
        ></i>
        <d-button *ngIf="!appId" bsStyle="common" (click)="onSelectApp()">
          {{ "kie.create.appSelect" | translate }}
        </d-button>
      </d-form-control>
    </d-form-item>

    <d-form-item *ngIf="configType === 'service' && type === 'create'">
      <d-form-label [required]="true">
        {{ "kie.create.service" | translate }}
      </d-form-label>
      <d-form-control>
        <span *ngIf="serviceId">{{ serviceId }}</span>
        <i
          *ngIf="serviceId"
          class="icon icon-delete app-delete"
          (click)="onDeleteService()"
        ></i>
        <d-button
          *ngIf="!serviceId"
          bsStyle="common"
          (click)="onSelectService()"
        >
          {{ "kie.create.serviceSelect" | translate }}
        </d-button>
      </d-form-control>
    </d-form-item>
    <d-form-item>
      <d-form-label [required]="true">
        {{ "kie.create.configName" | translate }}
      </d-form-label>
      <d-form-control>
        {{ configMessage }}
        <ng-container *ngIf="type === 'create'; else elseTemplate">
          <input
            type="text"
            dTextInput
            class="w-300"
            [placeholder]="'kie.create.configPlaceholder' | translate"
            autocomplete="off"
            name="configName"
            formControlName="configName"
            [dValidateRules]="configNameRules"
          />
        </ng-container>
        <ng-template #elseTemplate>
          <span>{{ formGroup.controls.configName.value }}</span>
        </ng-template>
      </d-form-control>
    </d-form-item>
    <d-form-item>
      <d-form-label [required]="false">
        {{ "kie.create.tag" | translate }}
      </d-form-label>
      <d-form-control>
        <div class="cse-mg-bottom-sm">
          <d-tag
            class="cse-mg-right-xs"
            *ngFor="let tag of tags; let i = index"
            [tag]="tag"
            [deletable]="configType === 'custom' && type === 'create'"
            [customViewTemplate]="customTag"
            (tagDelete)="onDeleteTag(i)"
          ></d-tag>
          <ng-template #customTag let-tag="tag">
            <span title="{{ tag }}">{{ tag }}</span>
          </ng-template>
        </div>
        <div *ngIf="configType === 'custom' && type === 'create'">
          <input
            type="text"
            dTextInput
            class="w-300"
            [placeholder]="'kie.create.tagKeyPlaceholder' | translate"
            [(ngModel)]="configTageKey"
            [ngModelOptions]="{ standalone: true }"
            [dValidateRules]="tagRules"
          />
          <input
            type="text"
            dTextInput
            class="w-300 cse-mg-left-xs"
            [placeholder]="'kie.create.tagValuePlaceholder' | translate"
            [(ngModel)]="configTageValue"
            [ngModelOptions]="{ standalone: true }"
            [dValidateRules]="tagRules"
          />
          <d-button
            icon="icon-add"
            [bsStyle]="'text-dark'"
            [bordered]="true"
            style="vertical-align: middle"
            (click)="onAddTage()"
          ></d-button>
        </div>
      </d-form-control>
    </d-form-item>
    <d-form-item>
      <d-form-label [required]="false">
        {{ "kie.create.size" | translate }}
      </d-form-label>
      <d-form-control>
        <d-button-group size="md">
          <d-button
            *ngFor="let item of configFormatItems"
            (click)="onConfigFormat(item.id)"
            [bsStyle]="configFormatId === item.id ? 'primary' : 'common'"
          >
            {{ item.text }}
          </d-button>
        </d-button-group>
      </d-form-control>
    </d-form-item>
    <d-form-item *ngIf="type === 'eidt'">
      <d-form-label [required]="false">
        {{ "kie.create.isAvailable" | translate }}
      </d-form-label>
      <d-form-control>
        <d-toggle formControlName="isAvailable"></d-toggle>
      </d-form-control>
    </d-form-item>

    <d-form-item>
      <d-form-label [required]="true">
        {{ "kie.create.configContent" | translate }}
      </d-form-label>
      <d-form-control>
        <ngx-monaco-editor
          #monacoEditor
          [options]="editorOptions"
          style="height: 400px; width: 80%"
          name="code"
          formControlName="code"
          [dValidateRules]="formRules.codeRules"
        ></ngx-monaco-editor>
      </d-form-control>
    </d-form-item>

    <d-form-operation class="form-operation">
      <d-button
        bsStyle="danger"
        circled="true"
        style="margin-right: 4px"
        (click)="onSubmit()"
        [disabled]="
          !!form.errorMessage ||
          (type === 'create' && configType !== 'custom' && !serviceId && !appId)
        "
        dTooltip
        [content]="form.errorMessage"
      >
        {{ "common.confirm" | translate }}
      </d-button>
      <d-button bsStyle="common" circled="true" (click)="cancel()">
        {{ "common.cancel" | translate }}
      </d-button>
    </d-form-operation>
  </form>
</div>
